﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Goooodbye.Presentation.WinFormsApp.UserControls
{
    public delegate void PageUpEventHandler(object sender, PageUpEventArgs e);
    public delegate void PageDownEventHandler(object sender, PageDownEventArgs e);
    public delegate void PageNumberChangedEventHandler(object sender, PageNumberChangedEventArgs e);

    public partial class Pagination : UserControl
    {
        private int pageNumber;
        private int pageCount;
        private int totalCount;
        private Font customFont;
        private Image previousBackground;
        private Image nextBackground;

        public int PageNumber
        {
            get => pageNumber;
            set
            {
                pageNumber = value;
                this.txtPageNum.Text = $"{pageNumber}";
            }
        }

        public int PageCount
        {
            get => pageCount;
            set
            {
                pageCount = value;
                this.lblPageCount.Text = $"/ {pageCount}";
            }
        }

        public int TotalCount
        {
            get => totalCount;
            set
            {
                totalCount = value;
                this.lblTotalCount.Text = $"总数: {totalCount}";
            }
        }

        public Font CustomFont
        {
            get
            {
                return customFont;
            }
            set
            {
                customFont = value;
                this.toolStrip1.Font = customFont;
                this.txtPageNum.Font = customFont;
                this.txtPageNum.Size = new Size(this.lblPageCount.Width, 30);
            }
        }

        public Image PreviousBackground
        {
            get { return previousBackground; }
            set
            {
                if (value != null)
                {
                    previousBackground = value;
                    this.btnPrevious.Image = previousBackground;
                }

            }
        }

        public Image NextBackground
        {
            get { return nextBackground; }
            set
            {
                if (value != null)
                {
                    nextBackground = value;
                    this.btnNext.Image = nextBackground;
                }
            }
        }

        public event PageUpEventHandler PageUp;
        public event PageDownEventHandler PageDown;
        public event PageNumberChangedEventHandler PageNumberChanged;

        public Pagination()
        {
            this.InitializeComponent();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (PageUp != null && PageNumber > 1)
            {
                PageNumber--;
                this.PageUp(this, new PageUpEventArgs(pageNumber));
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (PageDown != null && pageNumber < pageCount)
            {
                PageNumber++;
                this.PageDown(this, new PageDownEventArgs(pageNumber));
            }
        }

        private void txtPageNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (PageNumberChanged != null && e.KeyChar == (char)Keys.Enter)
            {
                if (Int32.TryParse(this.txtPageNum.Text, out int val) &&
                    val != pageNumber &&
                    val >= 1 &&
                    val <= pageCount)
                {
                    PageNumber = val;
                    this.PageNumberChanged(this, new PageNumberChangedEventArgs(pageNumber));
                }
            }
        }

        private void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            string val = String.Empty;
            for (int i = 0; i < this.txtPageNum.Text.Length; i++)
            {
                if (this.txtPageNum.Text[i] >= '0' && this.txtPageNum.Text[i] <= '9')
                {
                    val += this.txtPageNum.Text[i].ToString();
                }
            }
            this.txtPageNum.Text = val;
        }
    }

    public class PageUpEventArgs : EventArgs
    {
        public readonly int PageNumber;
        public PageUpEventArgs(int pageNumber)
        {
            this.PageNumber = pageNumber;
        }
    }

    public class PageDownEventArgs : EventArgs
    {
        public readonly int PageNumber;
        public PageDownEventArgs(int pageNumber)
        {
            this.PageNumber = pageNumber;
        }
    }

    public class PageNumberChangedEventArgs : EventArgs
    {
        public readonly int PageNumber;
        public PageNumberChangedEventArgs(int pageNumber)
        {
            this.PageNumber = pageNumber;
        }
    }
}
